/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.koubei.imenu.websocket;

import java.util.HashMap;

/**
 *
 * @author yunzi
 */
public class YunTable implements java.io.Serializable {

    private int orderedCourseId;
    private int menuId;//menuId，tableName，opener唯一标识“一桌”
    private String menuName;
    private String tableName;
    private String opener;
    private final HashMap<Integer, Course> ordered = new HashMap<Integer, Course>();
    private int amount;

    public YunTable(int menuId, String menuName, String tableName, String opener) {
        this.menuId = menuId;
        this.menuName = menuName;
        this.tableName = tableName;
        this.opener = opener;
    }

    public synchronized int getOrderedCourseId() {
        return orderedCourseId++;
    }

    public HashMap<Integer, Course> getOrdered() {
        return ordered;
    }

    public int order(int courseId, int price) {
        int tempId = this.getOrderedCourseId();
        synchronized (ordered) {
            ordered.put(tempId, new Course(tempId, menuId, courseId, price));
            this.amount = this.amount + price;
            return this.amount;
        }
    }

    public int remove(int orderedCourseId) {
        synchronized (ordered) {
            Course course = ordered.remove(orderedCourseId);
            this.amount = this.amount - course.getPrice();
            return this.amount;
        }
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public String getOpener() {
        return opener;
    }

    public void setOpener(String opener) {
        this.opener = opener;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public int getMenuId() {
        return menuId;
    }

    public void setMenuId(int menuId) {
        this.menuId = menuId;
    }

    public String getMenuName() {
        return menuName;
    }

    public void setMenuName(String menuName) {
        this.menuName = menuName;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final YunTable other = (YunTable) obj;
        if (this.menuId != other.menuId) {
            return false;
        }
        if ((this.tableName == null) ? (other.tableName != null) : !this.tableName.equals(other.tableName)) {
            return false;
        }
        if ((this.opener == null) ? (other.opener != null) : !this.opener.equals(other.opener)) {
            return false;
        }
        return true;
    }

    public String getStatus() {

        return "现有" + this.ordered.size() + "个菜，共" + this.amount + "元";

    }

    public String getStatusWithURLEncode() {
        try {
            return java.net.URLEncoder.encode(getStatus(), "utf-8");
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 59 * hash + this.menuId;
        hash = 59 * hash + (this.tableName != null ? this.tableName.hashCode() : 0);
        hash = 59 * hash + (this.opener != null ? this.opener.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return "YunTable{" + "menuId=" + menuId + ", tableName=" + tableName + ", opener=" + opener + '}';
    }

    class Course {

        private final int orderedCourseId;
        private int menuId;
        private int courseId;
        private int price;

        public Course(int orderedCourseId) {
            this.orderedCourseId = orderedCourseId;
        }

        public Course(int orderedCourseId, int menuId, int courseId, int price) {
            this.orderedCourseId = orderedCourseId;
            this.menuId = menuId;
            this.courseId = courseId;
            this.price = price;
        }

        public void setCourseId(int courseId) {
            this.courseId = courseId;
        }

        public int getCourseId() {
            return courseId;
        }

        public int getMenuId() {
            return menuId;
        }

        public void setMenuId(int menuId) {
            this.menuId = menuId;
        }

        public int getPrice() {
            return price;
        }

        public void setPrice(int price) {
            this.price = price;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Course other = (Course) obj;
            if (this.orderedCourseId != other.orderedCourseId) {
                return false;
            }
            return true;
        }

        @Override
        public int hashCode() {
            int hash = 3;
            hash = 19 * hash + this.orderedCourseId;
            return hash;
        }

        @Override
        public String toString() {
            return "ACourse{" + "menuId=" + menuId + ", courseId=" + courseId + '}';
        }
    }
}
